package com.sgb.kjwl.view.ui.activity.manage;

import android.annotation.SuppressLint;

import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCompanyTrendsDetailBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.kjwl.model.entity.manage.CompanyTradesDetailEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.sgb.kjwl.viewmodel.manage.CompanyTradesDetailModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.DateUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class CompanyTradesDetailActivity extends AppActivity {

    private ActivityCompanyTrendsDetailBinding binding;

    @Inject
    CompanyTradesDetailModel companyTradesDetailModel;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder()
                .appComponent(appComponent)
                .enterpriseManageModule(new EnterpriseManageModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_company_trends_detail);

        long id = getIntent().getLongExtra("id",0);

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        companyTradesDetailModel.getCompanyTradesDetail(map);

        binding.setViewModel(companyTradesDetailModel);

    }

    @SuppressLint("SetJavaScriptEnabled")
    public void getCompanyTradesDetail(CompanyTradesDetailEntity companyTradesDetailEntity){
        binding.setTradesDetail(companyTradesDetailEntity);

        if(!TextUtils.isEmpty(companyTradesDetailEntity.getDynamicTypeName())){
            TextView tv = new TextView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 26, 0);
            tv.setLayoutParams(params);
            tv.setBackgroundResource(R.drawable.corner2_6b78ff_79a2ff_bg);
            //tv.setPadding(4, 4, 4, 4);
            tv.setText(companyTradesDetailEntity.getDynamicTypeName());
            tv.setTextColor(getResources().getColor(R.color.white));
            tv.setTextSize(10f);
            binding.tags.addView(tv);
        }
        if(!TextUtils.isEmpty(companyTradesDetailEntity.getIndustryName())){
            TextView tv = new TextView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tv.setLayoutParams(params);
            tv.setBackgroundResource(R.drawable.corner2_fb7039_ffb553bg);
            tv.setTextSize(10f);
         //   tv.setPadding(4, 4, 4, 4);
            tv.setText(companyTradesDetailEntity.getIndustryName());
            tv.setTextColor(getResources().getColor(R.color.white));
            binding.tags.addView(tv);
        }


        binding.tvCompanyDetailName.setText(TextViewBindingAdapter.setForSpace(companyTradesDetailEntity.getUserName()+"     "+DateUtils.timestampTo13Date3(companyTradesDetailEntity.getCreateTime())));
        binding.webDescription.getSettings().setDefaultTextEncodingName("UTF-8");
        binding.webDescription.getSettings().setJavaScriptEnabled(true);
        String js = "<script type=\"text/javascript\">"+
                "var imgs = document.getElementsByTagName('img');" +
                "for(var i = 0; i<imgs.length; i++){" +
                "imgs[i].style.width = '100%';" +
                "imgs[i].style.height = 'auto';" +
                "}" +
                "</script>";
        binding.webDescription.loadDataWithBaseURL(null, companyTradesDetailEntity.getBody()+js, "text/html", "UTF-8", null);
    }
}
